/*
 * 
 * manages multiple items
 * one enabled and all others disabled at any given time
 * clicking an item will cause it to be enabled and all other disabled
 */
function SpriteSelect(_did, _callback, config) {
	if(!_did){
		alert('Cannot make Select without div id');
		return null;
	}else{
		var el=document.getElementById(_did);
		if(el===null) {
			alert(_did+' does not exist');
			return null;
		}
	}
	var currentValue=null;
	var className=config.className;
	var me=this;
	var callback=_callback;
	var did=_did;
	var ssid=did+'-id';
	var elements=[];
	this.enabled=true;
	this.isEnabled=function(){return me.enabled;};
	this.setEnabled=function(enabled) {
		me.enabled=enabled;
	}
	this.getVal=function() {
		for(var i in elements){
			var el=elements[i];
			if(i==currentValue){
				return el.val;
			}
		}
		return 'BOGUS';
	}
	this.setItem=function(val) {
		if(!me.enabled)return;
		var sel=null;
		for(var i in elements){
			var el=elements[i];
			var a=el.getElementsByTagName('a')[0];
			//alert('a.val='+a.val+'\n\nval='+val);
			if(a.val==val){
				el.className=className+'-'+i;
				sel=el;
			}else{
				el.className=className+'-disabled-'+i;
			}
		}
		return sel.val;
	};
	/*
	 * only forwards if the element is enabled
	 */
	this.callbackDelegate=function(e) {
		if(!me.enabled)return false;
		if (!e) var e = window.event;
		var targ;
		if (e.target) {
			targ = e.target;
		} else if (e.srcElement) {
			targ = e.srcElement;
		}
		var val=targ.val;
		var sel = me.setItem(val);
		callback(val);
		return false;
	};
	this.mkli=function(cls,val){
		var li=document.createElement('li');
		li.className=cls;
		li.onclick=me.callbackDelegate;
		var a=document.createElement('a');
		a.href='';
		a.val=val;
		a.title=val;
		li.appendChild(a);
		return li;
	};
	this.init=function() {
		var el=document.getElementById(did);
		var ul=document.createElement('ul');
		ul.id = ssid;
		ul.className=className;
		if(config.def === 'undefined') {
			alert('Missing config.def - specifies default element');
		}
		currentValue=config.def;
		for(var i in config.elements) {
			var cls=className;
			if(i!=currentValue){
				cls+='-disabled';
			}
			cls+='-'+config.elements[i].key;
			//alert('cls\n\n'+cls);
			var li=me.mkli(cls,config.elements[i].val);
			elements[elements.length]=li;
			ul.appendChild(elements[i]);
		}
		el.appendChild(ul);
	}
	me.init();
	me.setEnabled(me.enabled);
}
